<?php 
namespace app\home\controller;
class Content extends Common {

	public function index(){
		$id = \mylib\Input::param('id');
		$this -> assign('channel_id', $id);
		$top_channel = \Channel::top($id);
		$this -> assign('curpos', $top_channel['id']);

		$page = \mylib\Input::param('page');
		$page = $page?:1;
		$this -> assign('page', $page);

		if (!$channel = \Ebcms::db() -> get('channel','*',['id'=>$id])) {
			return $this -> error('栏目不存在！');
		}
		if (!$channel['status']) {
			return $this -> error('栏目未公开！');
		}
		$this -> assign('channel', $channel);
		$this -> assign('seo', [
			'title' => $channel['title'] . ' - ' . \Ebcms::config('site_name'),
			'keywords' => $channel['keywords'],
			'description' => $channel['description'],
		]);

		\Channel::parents($id, $parents);
		$tmp = array_reverse($parents);
		$pos = [];
		foreach ($tmp as $value) {
			$pos[] = [
				'title'=>$value['title'],
				'url'=>\mylib\Route::buildUrl('/home/content/index',['id'=>$value['id']])
			];
		}
		$this -> assign('pos', $pos);

		switch ($channel['type']) {
			case 'jump':
				return \mylib\Response::redirect(str_replace('__ROOT__', \Ebcms::root(), $channel['jump']));
				break;

			case 'channel':
				if ($channel['jump']) {
					return \mylib\Response::redirect(str_replace('__ROOT__', \Ebcms::root(), $channel['jump']));
				}
				return $this -> html($this -> fetch($channel['tpl']?:'channel'));
				break;

			case 'list':
				$pagenum = $channel['pagenum']?:20;
				$contents = \Ebcms::db() -> select('content', '*', [
					'status'=>1,
					'channel_id'=>$id,
					'ORDER'=>[
						'id'=>'DESC'
					],
					'LIMIT'=>[($page-1) * $pagenum, $pagenum]
				]);
				foreach ($contents as &$value) {
					$ext = $value['ext'];
					unset($value['ext']);
					$value['attr'] = json_decode($value['attr']?:'[]', true);
					$value = array_merge(json_decode($ext, true), $value);
				}
				unset($value);
				$this -> assign('contents', $contents);

				$total = \Ebcms::db() -> count('content', ['status'=>1,'channel_id'=>$id]);
				$this -> assign('pages', \Ebcms::page($total, $page, $pagenum));

				return $this -> html($this -> fetch($channel['tpl']?:'list'));
				break;

			case 'page':
				return $this -> html($this -> fetch($channel['tpl']?:'page'));
				break;
			
			default:
				return $this -> error('栏目设置错误！');
				break;
		}
	}
	
	public function detail(){
		$id = \mylib\Input::param('id');

		if (!is_numeric($id)) {
			$content = \Ebcms::db() -> get('content','*',['name'=>$id]);
		}else{
			if (!$content = \Ebcms::db() -> get('content','*',['id'=>$id])) {
				$content = \Ebcms::db() -> get('content','*',['name'=>$id]);
			}
		}

		if (!$content) {
			return $this -> error('内容不存在！');
		}
		if (!$content['status']) {
			return $this -> error('内容审核中..');
		}
		if ($content['jump']) {
			return \mylib\Response::redirect(str_replace('__ROOT__', \Ebcms::root(), $content['jump']));
		}
		
		$ext = json_decode($content['ext'], true);
		unset($content['ext']);
		$content['attr'] = json_decode($content['attr']?:'[]', true);
		$content = array_merge($ext, $content);
		\mylib\Hook::listen('content_assign', $content);
		$this -> assign('content', $content);
		
		\Channel::parents($content['channel_id'], $parents);
		$tmp = array_reverse($parents);
		$pos = [];
		foreach ($tmp as $value) {
			$pos[] = [
				'title'=>$value['title'],
				'url'=>\mylib\Route::buildUrl('/home/content/index',['id'=>$value['id']])
			];
		}
		$pos[] = [
			'title'=>isset($content['title'])?$content['title']:'暂无标题',
			'url'=>\mylib\Route::buildUrl('/home/content/detail',['id'=>$id])
		];
		$this -> assign('pos', $pos);

		$top_channel = \Channel::top($content['channel_id']);
		$this -> assign('curpos', $top_channel['id']);

		$this -> assign('seo',[
			'title'=>(isset($content['title'])?$content['title']:'标题') . ' - ' . \Ebcms::config('site_name'),
			'keywords'=>isset($content['keywords'])?$content['keywords']:'关键词',
			'description'=>isset($content['description'])?$content['description']:'简介',
		]);

		return $this -> html($this -> fetch($content['tpl']?:''));
	}

	public function search(){
		if ($q = \mylib\Input::get('q')) {
			
			$page = \mylib\Input::get('page');
			$page = $page?:1;
			$this -> assign('page', $page);

			$channels = \Ebcms::db() -> select('channel', '*', [
				'OR'=>[
					'title[~]'=>$q,
					'body[~]'=>$q,
				],
				'status'=>1
			]);
			$this -> assign('channels', $channels);

			$pagenum = 20;
			$contents = \Ebcms::db() -> select('content', '*', [
				'OR'=>[
					'title[~]'=>$q,
					'body[~]'=>$q,
				],
				'status'=>1,
				'ORDER'=>[
					'id'=>'DESC'
				],
				'LIMIT'=>[($page-1) * $pagenum, $pagenum]
			]);
			foreach ($contents as &$value) {
				$ext = $value['ext'];
				unset($value['ext']);
				$value['attr'] = json_decode($value['attr']?:'[]', true);
				$value = array_merge(json_decode($ext, true), $value);
			}
			unset($value);
			$this -> assign('contents', $contents);

			$total = \Ebcms::db() -> count('content', [
				'OR'=>[
					'title[~]'=>$q,
					'body[~]'=>$q,
				],
				'status'=>1
			]);
			$this -> assign('pages', \Ebcms::page($total, $page, $pagenum));
		}
		$this -> assign('q', $q);
		$this -> assign('curpos', 'search');
		$this -> assign('seo', [
			'title' => '搜索' . ($q ? '：' . $q : '') . ' - ' . \Ebcms::config('site_name'),
			'keywords' => '搜索',
			'description' => '搜索',
		]);

		$this -> assign('pos', [[
			'title'=>'搜索',
			'url'=>\mylib\Route::buildUrl('/home/content/search')
		]]);
		
		return $this -> html($this -> fetch());
	}
}